var fs = require('fs');


function upload (files ) {
    return new Promise(function (resolve, reject) {
        let path =  './' + files.destination;//文件路径
        let oldName =  path + files.filename;
        // 文件类型
        let temp = files.originalname.split('.');
        let fileType = temp[temp.length - 1];
        let lastName = '.' + fileType;
        // 构建图片名
        let newName = path + Date.now() + lastName;
        // 图片重命名
        fs.rename(oldName, newName, (err) => {
            if (err) {
                res.end(JSON.stringify({status:'102',msg:'文件写入失败'}));
            }else{
                var imageSrc = 'http://localhost:3000/typelist/' + newName;
                resolve(imageSrc)
            }
        });
    })
}

module.exports = upload;
